package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.domain.po.Student;
import com.campus.mapper.StudentMapper;
import com.campus.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public Student getStudentByCode(String code) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        return this.getOne(wrapper);
    }

    @Override
    public Student saveStudent(Student student) {
        Student existingStudent = this.getStudentByCode(student.getCode());
        if (existingStudent == null) {
            this.save(student);
            return student;
        } else {
            Long id = existingStudent.getId();
            BeanUtils.copyProperties(student, existingStudent);
            existingStudent.setId(id);
            this.updateById(existingStudent);
            return existingStudent;
        }
    }

    @Override
    public boolean deleteStudentById(Long id) {
        return this.removeById(id);
    }

    @Override
    public Student updateStudent(Student student) {
        this.updateById(student);
        return this.getById(student.getId());
    }

    @Override
    public Student getStudentById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean importStudents(List<Student> students) {
        return this.saveBatch(students);  // 批量保存
    }

    @Override
    public boolean auditStudent(Long studentId, String status) {
        Student student = this.getById(studentId);
        if (student != null) {
            student.setIsFirst(status);  // 设置状态为审核状态
            return this.updateById(student);
        }
        return false;
    }

    @Override
    public boolean auditStudents(List<Long> studentIds, String status) {
        List<Student> students = this.listByIds(studentIds);
        for (Student student : students) {
            student.setIsFirst(status);  // 设置状态为审核状态
        }
        return this.updateBatchById(students);  // 批量更新
    }
}
